<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users extends CI_Controller {

	function __construct() {

		parent::__construct();

		$this->load->model('t_users');

		$this->load->model('t_contacts');

		$this->load->library(array('form_validation','pagination'));

	}

	public function index()
	{	
		echo 'aaaa';
	}

/**
 * [register description] register user
 * @exit() json : [key] : [value]
 *                [username]:[-1 if null | 0 if lenght < 8 || > 30 | 1 if duplicate | 2 success]
 *                [email] : [-1 if null | 0 if lenght < 8 || > 30 | 1 if duplicate | 2 success]
 *                ['success']:[0|1]
 */
	public function register(){

		if($this->input->post()){

			$error = array();

			$success = 1;

			$data = $this->input->post();

			$username = $data[USERNAME];

			$password = $data[PASSWORD];

			$email = $data[EMAIL];

			$phone_number = $data[PHONE_NUMBER];

			$secret_key = $data[SECRET_KEY];

			// username
			if(empty($username)){

				$error[USERNAME] = -1;

				$success = 0;

			}elseif( strlen($username) < 8 || strlen($username) > 30 ){

				$error[USERNAME] = 0;

				$success = 0;

			}else{

				$query = $this->t_users->get_data_by_property(USERNAME, array('username = ' => $username));

				if(!empty($query)){

					$error[USERNAME] = 1;

					$success = 0;

				}else{
					$error[USERNAME] = 2;
				}
			}

			// password
			if(empty($password)){

				$error[PASSWORD] = -1;

				$success = 0;

			}elseif (strlen($password) < 6 || strlen($password) > 30) {

				$error[PASSWORD] = 0;

				$success = 0;

			}else{

				$error[PASSWORD] = 2;

			}

			//email
			if(empty($email)){

				$error[EMAIL] = -1;

				$success = 0;

			}elseif( strlen($email) < 10 ){

				$error[EMAIL] = 0;

				$success = 0;

			}else{


				$query = $this->t_users->get_data_by_property(EMAIL, array(EMAIL . ' = ' => $email));

				if(!empty($query)){

					$error[EMAIL] = 1;

					$success = 0;

				}else{
					$error[EMAIL] = 2;
				}
			}

			// phone_number
			if(empty($phone_number)){

				$error[PHONE_NUMBER] = -1;

				$success = 0;

			}elseif (strlen($phone_number) < 4) {

				$error[PHONE_NUMBER] = 0;

				$success = 0;
			}else{

				$error[PHONE_NUMBER] = 2;
			}

			// secret key
			if(empty($secret_key)){

				$error[SECRET_KEY] = -1;

				$success = 0;
			}elseif( strlen($secret_key) != 4){

				$error[SECRET_KEY] = 0;

				$success = 0;
			}else{
				$error[SECRET_KEY] = 2;
			}

			if($success == 1){

				$data[PASSWORD] = md5($data[PASSWORD]);

				$id = $this->t_users->set_data($data);

				$error = array();

				$error[SUCCESS] = $id;

				echo json_encode($error);
			}else{

				$error[SUCCESS] = 0;

				echo json_encode($error);
			}
		}else{

			echo "-1";
		}
	}
/**
 * [login description] check user login
 * @exit() [type] [description] : 0|1 : login success exit() 1 else exit() 0
 */
	public function login(){

		$data = array();

		// check post data
		if($this->input->post()){

			$post = $this->input->post();

			if(!empty($post[USERNAME])){

				$username = $post[USERNAME];
			}else{

				$data[SUCCESS] = 0;

				$data[LOG][USERNAME] = 'Username is null';

			}

			if(!empty($post[PASSWORD])){

				$password = $post[PASSWORD];	
			}else{

				$data[SUCCESS] = 0;

				$data[LOG][PASSWORD] = 'Password is null';
			}

			if(isset($data[SUCCESS])){

				echo json_encode($data);exit();
			}

			$query = $this->t_users->get_data_by_property('*', array(USERNAME => $username, PASSWORD => md5($password)));

			// check query
			if(!empty($query) && is_array($query)){

				unset($query[0][PASSWORD]);

				$data[SUCCESS] = 1;

				unset($data[LOG]);

				$data[DATA] = $query;

				echo json_encode($data);

				exit();
			}else{

				$data[SUCCESS] = 0;

				$data[LOG] = 'The username or password is incorrect';

				echo json_encode($data);

				exit();
			}
		}else{

			$data[SUCCESS] = 0;

			$data[LOG] = 'post value is null';

			echo json_encode($data);

			exit();
		}
	}

/**
 * get user by id
 * @exit() mix    
 */
	public function get_user_by_id(){

		$data = array();

		// check post data
		if($this->input->post()){
			
			$data = $this->input->post();

			$select = '';

			$id = -1;

			foreach ($data as $key =>$value) {

				if( $key == USER_ID && !empty($key)){

					$id = $value;
				}else{

					$select .= $key . ',';
				}
			}

			if( $id == -1){
				
				$data[SUCCESS] = 0;

				$data[LOG] = 'id value is invalid';

				echo json_encode($data);

				exit();
			}

			if(empty($select)){

				$select = '*';
			}else{

				$select = substr($select, 0, strlen($select) -1);
			}

			$query = $this->t_users->get_data_by_id($id, $select);

			if($query != 0 && $query != -1){

				$data[SUCCESS] = 1;

				$data[DATA] = $query;
				
			}else{

				$data[SUCCESS] = 0;

				if($query == 0){

					$data[LOG] = 'No user by id';
				}else{

					$data[LOG] = 'id invalid';
				}
			}

			echo json_encode($data);

			exit() ;
		}else{

			$data[SUCCESS] = 0;

			$data[LOG] = 'value post is null';

			echo json_encode($data);

			exit();
		}
	}


/**
 * change password user
 * @exit() [mix]
 */
	public function changepassword(){

		$data = array();

		if( $this->input->post()){

			$post = $this->input->post();

			if(!empty($post[USER_ID])){

				$user_id = $post[USER_ID];
			}else{

				$data[SUCCESS] = 0;

				$data[LOG][USER_ID] = 'user_id is null';
			}

			if(!empty($post[PASSWORD])){

				$password = $post[PASSWORD];
			}else{

				$data[SUCCESS] = 0;

				$data[LOG][PASSWORD] = 'Password is null';
			}

			if(!empty($post[NEWPASSWORD])){

				$newpassword = $post[NEWPASSWORD];
			}else{

				$data[SUCCESS] = 0;

				$data[LOG][NEWPASSWORD] = 'New password is null';
			}

			if(isset($data[SUCCESS])){

				echo json_encode($data); exit();
			}

			$query = $this->t_users->update_data_by_property(array(PASSWORD => md5($newpassword)), array(USER_ID => $user_id, PASSWORD => md5($password)));

			if($query){

				$data[SUCCESS] = 1;

				$data[DATA] = 'Change password success';
			}else{

				$data[SUCCESS] = 1;

				$data[DATA] = 'password and new password is match';
			}

			echo json_encode($data); exit();

		}else{

			$data[SUCCESS] = 0;

			$data[LOG] = 'Value post is null';
		}

		echo json_encode($data); exit();
	}

/**
 * change user password 
 * @return json
 */
	public function forgotpassword(){

		$data = array();

		if($this->input->post()){

			$post = $this->input->post();

			if(!empty($post[EMAIL])){

				$email = $post[EMAIL];

				$query = $this->t_users->get_data_by_property(USER_ID, array(EMAIL => $email));

				if(empty($query)){

					$data[SUCCESS] = 0;

					$data[LOG] = 'Missing email';

					echo json_encode($data); exit();
				}elseif($query == -1){

					$data[SUCCESS] = 0;

					$data[LOG] = 'No have user by email';

					echo json_encode($data); exit();
				}

				// $reset_password = $query['reset_password'];

				$random_code=$this->random_password();				

				$this->load->model('T_email_template');

				require_once("parse.php");
				
				$get_parse_email = new ParserEmailTemplate();

				$data_sendmail = array("NEWPASSWORD" => $random_code);
				
				$get_parse_email->get_template_password($data_sendmail);

				$parse_email = $get_parse_email->public_template['template'];

				$parse_subject = $get_parse_email->public_template['subject'];

				if (!empty($parse_email) && !empty($parse_subject) )
				{
					require_once("email.php");

					$sendemail = new Email();

					$arraymail = array('SUBJECT' => $parse_subject, 'TO' => $email , 'BODY' => $parse_email );

					// send mail
					if($sendemail->send_mail($arraymail)){

						$update_pass = $this->t_users->update_data_by_id(array(PASSWORD => md5($random_code)), $query[0][USER_ID]);

						if(empty($update_pass) || $update_pass == false){

							$data[SUCCESS] = 0;

							$data[LOG] = 'update password fail';

							echo json_encode($data); exit();
						}

						$data[SUCCESS] = 1;

						$data[DATA] = 'Send mail success';
					}else{

						$data[SUCCESS] = 0;

						$data[DATA] = 'Can not send mail';
					}
				}else{

					$data[SUCCESS] = 0;

					$data[DATA] = 'Server mail has problem';

				}
			}else{

				$data[SUCCESS] = 0;

				$data[LOG] = 'user id is null';
			}

			echo json_encode($data); exit();

		}else{

			$data[SUCCESS] = 0;

			$data[LOG] = 'Value post is null';

			echo json_encode($data); exit();
		}
	}



	/**
	 * change security key 
	 * @return [type] [description]
	 */
	public function changesecuritykey(){

		$data = array();

		if($this->input->post()){

			$post = $this->input->post();

			if(!empty($post[USER_ID])){

				$user_id = $post[USER_ID];
			}else{

				$data[SUCCESS] = 0;

				$data[LOG] = 'user id is null';
			}

			if(!empty($post[SECRET_KEY])){

				$new_secret_key = $post[SECRET_KEY];

				if(strlen($new_secret_key) != 4){
					$data[SUCCESS] = 0;

					$data[LOG] = 'lenght of secret key != 4';
				}
			}else{

				$data[SUCCESS] = 0;

				$data[LOG] = 'secret key is null';
			}

			if(isset($data[SUCCESS])){

				echo json_encode($data); exit();
			}

			// check user exist
			$query = $this->t_users->get_data_by_id($user_id);

			if(empty($query)){

				$data[SUCCESS] = 0;

				$data[LOG] = 'user id is null';
				
			}elseif ($query == -1) {

				$data[SUCCESS] = 0;

				$data[LOG] = 'No have user by user id';

			}else{

				$update_user = $this->t_users->update_data_by_id(array(SECRET_KEY => $new_secret_key), $user_id);

				if($update_user){

					$data[SUCCESS] = 1;

					$data[DATA] = 'change secret key success';
				}else{

					$data[SUCCESS] = 1;

					$data[DATA] = 'secret key is match';
				}
			}

			echo json_encode($data); exit();

		}else{

			$data[SUCCESS] = 0;

			$data[LOG] = 'Value post is null';

			echo json_encode($data); exit();
		}
	}


	public function forgotsecuritykey(){

		$data = array();

		if($this->input->post()){

			$post = $this->input->post();

			if(!empty($post[USER_ID])){

				$user_id = $post[USER_ID];

				$query = $this->t_users->get_data_by_id($user_id, SECRET_KEY . ',' . EMAIL);

				if(empty($query)){

					$data[SUCCESS] = 0;

					$data[LOG] = 'Missing user id';

					echo json_encode($data); exit();
				}elseif($query == -1){

					$data[SUCCESS] = 0;

					$data[LOG] = 'No have user by user id';

					echo json_encode($data); exit();
				}

				// $reset_password = $query['reset_password'];

				$this->load->model('T_email_template');

				require_once("parse.php");
				
				$get_parse_email = new ParserEmailTemplate();

				$data_sendmail = array("NEWSECRETKEY" => $query[SECRET_KEY]);
				
				$get_parse_email->get_template_secretkey($data_sendmail);

				$parse_email = $get_parse_email->public_template['template'];

				$parse_subject = $get_parse_email->public_template['subject'];

				if (!empty($parse_email) && !empty($parse_subject) )
				{
					require_once("email.php");

					$sendemail = new Email();

					$arraymail = array('SUBJECT' => $parse_subject, 'TO' => $query[EMAIL] , 'BODY' => $parse_email );

					// send mail
					if($sendemail->send_mail($arraymail)){

						$data[SUCCESS] = 1;

						$data[DATA] = 'Send mail success';
					}else{

						$data[SUCCESS] = 0;

						$data[DATA] = 'Can not send mail';
					}
				}else{

					$data[SUCCESS] = 0;

					$data[DATA] = 'Get template has problem';

				}
			}else{

				$data[SUCCESS] = 0;

				$data[LOG] = 'user id is null';
			}

			echo json_encode($data); exit();

		}else{

			$data[SUCCESS] = 0;

			$data[LOG] = 'Value post is null';

			echo json_encode($data); exit();
		}

	}

	/**
	 * [random_password description]
	 * @return string [description]
	 * format same : Abc1a5g - Lenght = 9
	 */
	public function random_password()
	{
	   	mt_srand((double) microtime() * 10000); //optional for php 4.2.0 and up.

	   	$charid=strtoupper(md5(uniqid(rand(), true)));

	   	$uuid=substr($charid, 0, 6);

	   	return $uuid;
	}

}